//
//  KSImageUtilCell.swift
//  PigManager
//
//  Created by Ken on 2019/1/21.
//  Copyright © 2019 Much. All rights reserved.
//

import UIKit

class KSImageUtilCell: UICollectionViewCell {
    
    var imgBtn:UIButton!
    var deleteBtn:UIButton!
    
    let loadView = UIImageView().extHidden(hidden: true)
        .extImage(img: UIImage(named: "loadingMaskImage.png"))
    
    convenience required init(coder : NSCoder){
        self.init()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initUtil()
    }
    
    func initUtil() {
        
        extClipsToBounds(bounds: true)
        
        imgBtn = UIButton(frame: self.bounds)
        imgBtn.setTitleColor(.darkGray, for: .normal)
        imgBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        imgBtn.titleLabel?.numberOfLines = 0
        imgBtn.imageView?.contentMode = .scaleAspectFit
        imgBtn.imageView?.clipsToBounds = true
     
        self.addSubview(imgBtn)
        
//        loadView.image = loadCoverImg()
        addSubview(loadView)
        loadView.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets.zero)
        }
        
        deleteBtn = UIButton(frame: CGRect(x:(self.ks_width - 25),y:0,width:25,height:25))
        deleteBtn.setBackgroundImage(UIImage(named:"ksphoto_delete"), for: .normal)
        self.addSubview(deleteBtn)
    }
    
    
    func loadCoverImg(color: UIColor = .init(white: 0, alpha: 0.4)) -> UIImage {
        let coverView = UIView()
        coverView.backgroundColor = .clear
        coverView.frame = .init(x: 0, y: 0, width: 100, height: 100)
 
        
        let iconView = UIView()
        iconView.backgroundColor = .init(white: 0, alpha: 0.4)
        iconView.frame = coverView.bounds
        
        
        let width = iconView.frame.size.width/2
        let maskPath = UIBezierPath.init(rect: iconView.bounds)
        let maskCyclePath = UIBezierPath.init(arcCenter: .init(x: width, y: width), radius: width - 10, startAngle: 0, endAngle: 2*CGFloat.pi, clockwise: false)
        maskPath.append(maskCyclePath)
        
        let mask = CAShapeLayer()
        mask.path = maskPath.cgPath
        
        iconView.layer.mask = mask
        
        coverView.addSubview(iconView)
        
        
        let cycleWidth = width - 14
        let center = CGPoint(x: cycleWidth, y: cycleWidth)
        let radius: CGFloat = cycleWidth
        let startAngle = -CGFloat.pi/2
        let endAngle = CGFloat.pi/5
        
        let cyclePath = UIBezierPath()
        cyclePath.move(to: center)
        cyclePath.addArc(withCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: false)
        cyclePath.close()
        cyclePath.fill()
        
        let layer = CAShapeLayer()
        layer.fillColor = UIColor.init(white: 0, alpha: 0.4).cgColor
        layer.strokeColor = UIColor.clear.cgColor
        layer.path = cyclePath.cgPath
        
        let cycleView = UIView()
        cycleView.backgroundColor = .clear
        cycleView.frame = CGRectMake(width - cycleWidth, width - cycleWidth, cycleWidth*2, cycleWidth*2)
        cycleView.layer.addSublayer(layer)
        
        coverView.addSubview(cycleView)
        
        
        let render = UIGraphicsImageRenderer(size: coverView.bounds.size)
        return render.image { _ in
            coverView.drawHierarchy(in: coverView.bounds, afterScreenUpdates: true)
        }
    }
}
